package com.example.origin.finance.service.impl;

import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONUtil;
import com.example.origin.finance.dto.CnInfoReport;
import com.example.origin.finance.service.AbstractMultiStoreService;
import com.example.origin.resttemplate.util.RestUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Slf4j
@Service
public class CnInfoReportMultiStoreService extends AbstractMultiStoreService<CnInfoReport> {

    @Override
    protected void fillMap(String code, MultiValueMap<String, CnInfoReport> linkedMultiValueMap) {
        String orgId = getOrgId(code);
        if (orgId == null) {
            return;
        }
        Map<String, String> headerMap = new HashMap<>();
        headerMap.put("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");


        String u1 = "http://www.cninfo.com.cn/new/hisAnnouncement/query";
        MultiValueMap<String, String> body = new LinkedMultiValueMap<>();
        body.add("stock", code + "," + orgId);
        body.add("tabName", "fulltext");
        body.add("pageSize", "500");
        body.add("pageNum", "1");
        body.add("category", "category_ndbg_szsh;category_bndbg_szsh;category_yjdbg_szsh;category_sjdbg_szsh;");
        // stock=300033,9900008416&tabName=fulltext&pageSize=300&pageNum=1&category=category_ndbg_szsh;category_bndbg_szsh;category_yjdbg_szsh;category_sjdbg_szsh;

        ResponseEntity<Object> response;
        try {
            response = RestUtil.restPost(restTemplate, u1, body, headerMap, ParameterizedTypeReference.forType(Map.class));
        } catch (Exception e) {
            log.info("error",e);
            return;
        }

        JSONArray jsonArray = JSONUtil.parseObj(response).getJSONObject("body").getJSONArray("announcements");
        if (jsonArray == null) {
            return;
        }
        HashMap<String, String> paramMap = new HashMap<>();
        setParamMap(code, paramMap);

        for (int i = 0; i < jsonArray.size(); i++) {
            Map map1 = jsonArray.get(i, Map.class);
            CnInfoReport cnInfoReport = new CnInfoReport();
            cnInfoReport.setCode(code);
            cnInfoReport.setSymbol(paramMap.get("symbol"));
            cnInfoReport.setAnnouncementTitle(map1.get("announcementTitle").toString());
            cnInfoReport.setAnnouncementTime(new Date(Long.parseLong(map1.get("announcementTime").toString())));
            cnInfoReport.setShortTitle(map1.get("shortTitle").toString());
            linkedMultiValueMap.add(code, cnInfoReport);
        }
    }

    private String getOrgId(String code) {
        String u1 = "http://www.cninfo.com.cn/new/information/topSearch/detailOfQuery";
        MultiValueMap<String, String> body = new LinkedMultiValueMap<>();
        body.add("keyWord", code);
        body.add("maxSecNum", "10");
        body.add("maxListNum", "5");
        Map<String, String> headerMap = new HashMap<>();
        headerMap.put("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
        ResponseEntity<Object> response;
        try {
            response = RestUtil.restPost(restTemplate, u1, body, headerMap, ParameterizedTypeReference.forType(Map.class));
        } catch (Exception e) {
            log.info("error",e);
            return null;
        }
        Map map = JSONUtil.parseObj(response).getJSONObject("body").getJSONArray("keyBoardList").get(0, Map.class);
        String orgId = map.get("orgId").toString();
        return orgId;
    }
}
